<template>
  <div>
    
     <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
       
        <el-form-item label="实验名">
          <el-input
            v-model="experiment"
            placeholder="请输入实验名"
          ></el-input>
        </el-form-item>
         <el-form-item label="实验室位置">
          <!-- <el-input
            v-model="local"
            placeholder="请输入实验室位置"
          ></el-input> -->
           <el-select v-model="local" placeholder="请选择实验室">
              <el-option
                v-for="item in labors"
                :key="item.id"
                :label="item.name"
                :value="item.local">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="实验日期">
           <el-date-picker
            v-model="date"
              type="date"
              value-format="yyyy-MM-dd" 
              placeholder="选择日期">
            </el-date-picker>
        </el-form-item>

        <el-form-item label="实验时间">
            <el-select v-model="time" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="load(1)">查询</el-button>
          <el-button type="info" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>

      <!--按钮-->
      <el-row>
        <el-button type="primary" size="medium" @click="formVisible = true;">+ 新增实验计划</el-button>
      </el-row>
    </el-card>

    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="教师号"> </el-table-column>
      <el-table-column prop="experiment" label="实验名"> </el-table-column>
      <el-table-column prop="local" label="实验室位置"> </el-table-column>
      <el-table-column prop="date" label="实验日期"> </el-table-column>
      <el-table-column prop="time" label="时间"> </el-table-column>
      <el-table-column prop="file" label="文件地址"> </el-table-column>
      <el-table-column label="状态"> 
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state =='P'">通过</el-tag>
          <el-tag type="danger" v-if="scope.row.state == 'NP'">未通过</el-tag>
        </template>
      </el-table-column>
    </el-table>

       <el-row>
      <div class="block" style="margin:10px 0">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
    </el-row>

        <el-dialog title="添加实验计划" :visible.sync="formVisible"  width="40%">
        <el-form :model="user" label-width="80px" style="padding-right : 20px" :rules="rules">
            <el-form-item label="教师号" prop="username">
            <el-input v-model="user.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="实验名" prop="experiment">
            <el-input v-model="user.experiment"></el-input>
            </el-form-item>
         
            <el-form-item label="实验室位置" prop="local">
            <!-- <el-input v-model="user.local"></el-input> -->
              <el-select v-model="user.local" placeholder="请选择实验室">
              <el-option
                v-for="item in labors"
                :key="item.id"
                :label="item.name"
                :value="item.local">
              </el-option>
            </el-select>
            </el-form-item>

            <el-form-item label="实验日期" prop="date">
           <el-date-picker
            v-model="user.date"
              type="date"
              value-format="yyyy-MM-dd" 
              placeholder="选择日期">
            </el-date-picker>
        </el-form-item>

        <el-form-item label="实验计划文件">
            <el-upload
              class="upload-demo"
              action="http://localhost:8080/files/"
              :limit="1"
              :file-list="fileList"
              :on-success="handFileSuccess"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">上传实验计划文件</div>
            </el-upload>
        </el-form-item>

            <el-form-item>
            <el-button type="primary" @click="save()">提交</el-button>
            <el-button @click="formVisible = false">取消</el-button>
            </el-form-item>
      </el-form>
    </el-dialog>



  </div>
</template>

<script>
export default {
    name:'TPlanMsg',
    data () {
        return {
          user: JSON.parse(localStorage.getItem("honey-user") || "{}"),
          labors : [],
          options: [{
          value: '08:20-09:55',
          label: '08:20-09:55'
        }, {
          value: '10:10-11:45',
          label: '10:10-11:45'
        }, {
          value: '10:10-12:30',
          label: '10:10-12:30'
        }, {
          value: '13:20-14:55',
          label: '13:20-14:55'
        }, {
          value: '15:10-16:45',
          label: '15:10-16:45'
        },{
          value: '15:10-17:30',
          label: '15:10-17:30'
        },{
          value: '18:15-19:50',
          label: '18:15-19:50'
        },{
          value: '18:15-20:35',
          label: '18:15-20:35'
        }

        ],
        tableData:[], // 所有数据
        page : 1, // 当前的页码
        pageSize : 5, // 每页显示的个数
        total : 0,
        experiment : '',
        local : '',
        date : '',
        time: '',
        formVisible : false,
        fileList : [],
        rules: {
          
          experiment: [
            { required: true, message: '请输入实验名', trigger: 'blur' },
            { min: 1, max: 150, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          
          ],
          local : [
            { required: true, message: '请输入实验室位置', trigger: 'blur' }
          ],
          date : [
            { required: true, message: '请输入日期', trigger: 'blur' }
          ]

      }
            
        }
    },
    created () {
        this.load(1);
        this.loadLabors();
    },
    methods: {
      loadLabors(){
            this.$request.get('/labors/selectAll').then((result) => {
                this.labors = result.data;
            }).catch((err) => {
                console.log(err);
            });
        },
        load(page){ // 分页查询
        if (page) {
            this.page = page;
        }
        this.$request.get('/plans',{
            params : {
                page : this.page,
                pageSize : this.pageSize,
                username: this.user.username,
                experiment: this.experiment,
                local : this.local,
                date : this.date,
                time : this.time
            }
        }).then((res) => {
            this.total = res.data.total;
            this.tableData = res.data.rows;
        }).catch((err) => {
            console.log(err);
        });
    },
    handleCurrentChange(page){
      //  this.page = page;
        this.load(page);
    },
    reset(){
        this.experiment = '';
        this.local = '';
        this.date = '';
        this.time = '';
        this.load(1);
    },
    handFileSuccess(response, file,fileList){
      this.user.file = response.data;
      console.log(response,file,fileList);
    },
    save(){
      this.$request.post("/plans",this.user).then((res) => {
        if(res.code === 1){
                this.$message({
                //message: '登陆成功',
                message:res.msg,
                type: 'success'
            });
            this.load(1);
            this.formVisible = false;
            }else{
                 this.$message({
                //message: '登陆失败',
                message: res.msg,
                type: 'warning'
            });
            }
      }).catch((err) => {
        console.log(err);
      });
    }
    }
}

</script>

<style>

</style>